package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.jpa.model.NoticeLine;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.quality.entity.*;
import com.mxpio.erp.common.quality.service.InventoryInspectApplyService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class InventoryInspectApplyServiceImpl extends BaseServiceImpl<InventoryInspectApply> implements InventoryInspectApplyService {
    @Override
    @Transactional
    public Result<? extends Order> agreeByBizNo(String bizNo) {
        InventoryInspectApply po = this.getById(InventoryInspectApply.class, bizNo);
        List<InventoryInspectApplyLine> lineList = JpaUtil.linq(InventoryInspectApplyLine.class).equal("bizNo", po.getBizNo()).list();
        if (lineList.size()==0){
            return Result.error("库存检验申请明细为空，无法生成对应的验收单！");
        }
        if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())) {
            Set<String> itemCodeSet = lineList.stream().map(InventoryInspectApplyLine::getItemCode).collect(Collectors.toSet());
            List<Item> itemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
            Map<String, Item> itemMap = JpaUtil.index(itemList,"itemCode");//查询物料
            List<OrderExcuteLineVo> noticeVos = new ArrayList<>();
            //包装成订单执行时的对象列表
            for (InventoryInspectApplyLine line:lineList){
                OrderExcuteLineVo excuteLineVo = new OrderExcuteLineVo();
                excuteLineVo.setBizOrderNo(po.getBizNo());
                excuteLineVo.setBizOrderlineNo(line.getLineNo());
                excuteLineVo.setItemCode(line.getItemCode());
                excuteLineVo.setLineQuantity(line.getQuantity());
                excuteLineVo.setLotNo(line.getLotNo());
                excuteLineVo.setSubLotIndex(line.getSubLotIndex());
                excuteLineVo.setQuantity(line.getQuantity());
                excuteLineVo.setWhCode(line.getWhCode());
                excuteLineVo.setMemo(line.getMemo());
                noticeVos.add(excuteLineVo);
            }
            Map<String,Notice> noticeMap = new HashMap<>();
            Map<String,List<OrderExcuteLineVo>> voMap = new HashMap<>();
            for(OrderExcuteLineVo orderExcuteVo : noticeVos) {
                Notice notice = noticeMap.get(orderExcuteVo.getWhCode());
                if(notice == null) {
                    notice = new InventoryQualityOrder();//库存质检验收单

                    notice.setBizOrderNo(orderExcuteVo.getBizOrderNo());
                    notice.setNoticeDate(new Date());
                    notice.setWhCode(orderExcuteVo.getWhCode());
                    notice.setAccessType(OrderEnums.AccessType.IN.getCode());
                    notice.setPickingType(NoticeStatusEnums.NoticeType.IIA_IN.getCode());//库存质检申请-转移出
                    notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CREATE.getStatusCode());
                    noticeMap.put(orderExcuteVo.getWhCode(), notice);
                    voMap.put(orderExcuteVo.getWhCode(), new ArrayList<OrderExcuteLineVo>());
                }
                voMap.get(orderExcuteVo.getWhCode()).add(orderExcuteVo);
            }
            // 生成验收单明细
            for(Map.Entry<String,Notice> entry : noticeMap.entrySet()) {
                Notice notice = entry.getValue();
                JpaUtil.save(notice);
                int lineNo = 0;
                for(OrderExcuteLineVo vo : voMap.get(entry.getKey())) {
                    lineNo++;
                    NoticeLine line = notice.createLine(vo);
                    line.setLineNo(lineNo+"");
                    Item item = JpaUtil.linq(Item.class).idEqual(vo.getItemCode()).findOne();
                    line.setItemName(item.getItemName());
                    line.setItemSpec(item.getItemSpec());
                    line.setUnitCode(item.getUnitCode());
                    line.setDrawingNo(item.getDrawingNo());
                    line.setSubLotIndex(vo.getSubLotIndex());
                    JpaUtil.save(line);
                }
            }
            po.setOrderStatus(OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode());
            po.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
            JpaUtil.update(po);
            return Result.OK("确认成功，已生成对应的验收单",po);
        } else {
            return Result.error("非审核中状态不可审核");
        }
    }
}
